//    Travel US - For users of travel.usairways.com
//    intended for US Airways employees and their dependents
//    This app will help you track your flights and display
//    Availability, flight status and other non-rev info
//    Copyright (C) 2014  Renats Stozkovs
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

package com.bc004346.travelus.DataAccess;

import com.bc004346.travelus.Model.Domain.Flight;
import com.bc004346.travelus.Model.TravelUsException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.SocketTimeoutException;

import static com.bc004346.travelus.Utility.Utility.DATE_DASHES;
import static com.bc004346.travelus.Utility.Utility.log;

/**
 * Created by Renats on 2/5/14.
 * Screen scraper from US Airways site
 */
public class UsAirwaysScreenScraper {

    //<editor-fold desc="Members">

    private static final String URI_FLIGHT_STATUS = "http://mobile.usairways.com/mt/flights-mobile.usairways.com/FlightStatus.aspx?fs_by=FlightNumber&fs_num=%d&fs_date=%s&un_jtt_redirect";
    private static final String USER_AGENT = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.3; WOW64; Trident/7.0)";
    private static final String FLIGHT_TABLE = "div[class=lmb12b_l]";
    private static final String ORIGIN_ELEMENT = "span[class^=ltblue]";
    private static final String SPAN = "span";

    //</editor-fold>

    //<editor-fold desc="Properties">

    public static int getTimeout() {
        return 30000;
    }

    //</editor-fold>


    public static Flight getFlightStatus(Flight flight) throws IOException, TravelUsException {

        log(String.format("starting... flight number %d origin %s, date %s",
                flight.getFlightNumber(),
                flight.getOrigin(),
                DATE_DASHES.format(flight.getDeparture())));

        Document doc;
        try {
            doc = Jsoup
                    .connect(String.format(URI_FLIGHT_STATUS,
                            flight.getFlightNumber(),
                            DATE_DASHES.format(flight.getDeparture())))
                    .userAgent(USER_AGENT)
                    .timeout(5000).get();
        } catch (SocketTimeoutException e) {
            throw new TravelUsException("US Airways website seems to be unresponsive at the moment");
        }
        Elements elements = doc.select(FLIGHT_TABLE);
        for (Element element : elements) {
            Element departure = element.select(ORIGIN_ELEMENT).first();
            if (departure != null && departure.text().equals(flight.getOrigin())) {
                flight.setFlightStatus(element.select(SPAN).last().text());
                break;
            }
        }


        return flight;
    }
}
